const ip = require('../utils/ipUtils')
const config = require('../config')
const path = require('path')
// const fs = require('fs')
const mysql = require('../db/mysql')
const swiperDao = require('../sql/swiperDao')
const fs = require('fs')
const dateUtils = require('../utils/dateUtils')

const url = ip.base + '/swiper/file/';

// 获取首页轮播图
exports.index = async (req,res)=>{
    try {
        let row = await mysql(swiperDao.selectAllPublic);
        row = row.map(item=>{
            item.img_url = url + item.img_url;
            return item;
        })
        res.success(row);
    }catch (e) {
        console.log(e)
        res.status(500).fail('服务器错误')
    }
}

// 获取轮播图文件
exports.getFile = (req,res)=>{
    try {
        let options = {
            root: path.join(__dirname, '../file/swiper/')
        };
        let fileName = req.params.name;
        res.sendFile(fileName, options);
    }catch (e) {
        console.log(e)
        res.status(500).fail('服务器错误')
    }
}

// 获取所有轮播图
exports.allSwiper = async (req,res)=>{
    try {
        let num = (req.query.currentPage-1)*req.query.count||0;
        let count = +req.query.count||3;
        let row = await mysql(swiperDao.selectAll, [num, count]);
        let total = await mysql(swiperDao.total);
        row = row.map(item=>{
            item.img_url = url + item.img_url;
            return item;
        })
        res.success({
            data: row,
            total: total[0].total
        });
    }catch (e) {
        console.log(e)
        res.status(500).fail('服务器错误')
    }
}

// 上传轮播图
exports.upLoad = (req,res)=>{
    try {
        const file = req.file;
        // 获取文件的后缀名
        const endName = path.extname(file.originalname);
        // 拼接新的文件名称
        let fileName =  file.filename + endName;
        let newFileName = path.join(__dirname, '../file/swiper/') + fileName;
        // 将旧的文件名称改成新名称
        fs.rename(file.path, newFileName, async (err)=>{
            try{
                if(err) return res.status(500).fail("服务器错误", 500);
                let date = dateUtils.getNowDate();
                let row = await mysql(swiperDao.upLoad, [fileName, req.body.sort_count??99, req.body.url, date])
                if(row.affectedRows==1) return res.success({
                    img_url: url + fileName
                }, '添加成功')
                else return res.fail('添加成功')
            }catch (e) {
                res.status(500).fail('服务器错误')
            }
        })
    }catch (e) {
        console.log(e)
        res.status(500).fail('服务器错误')
    }
}

// 修改轮播图是否显示与跳转链接
exports.updateType = async (req,res)=>{
    try {
        let body = {};
        req.body.type? body.type = 1:body.type = 0;
        req.body.url? body.url = req.body.url:null;
        const row1 = await mysql(swiperDao.updateType, [body, req.body.id]);
        if(row1.affectedRows==1) return res.success({}, '修改成功');
        else return res.fail('修改失败')
    }catch (e) {
        console.log(e)
        res.status(500).fail('服务器错误')
    }
}

// 删除轮播图
exports.delete = async (req,res)=>{
    try {
        const row = await mysql(swiperDao.selectById, [req.body.id]);
        if(row.length==1){
            const row1 = await mysql(swiperDao.delete, [req.body.id]);
            try{
                // 删除本地文件
                fs.unlinkSync(path.join(__dirname, '../file/swiper/') + row[0].img_url);
            }catch (e) {
                console.log(e)
            }
            if(row1.affectedRows==1) return res.success({}, '删除成功');
            else return res.fail('删除失败')
        }
        else return res.fail('删除失败')
    }catch (e) {
        console.log(e)
        res.status(500).fail('服务器错误')
    }
}

